package com.myhouse.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myhouse.biz.DistrictService;
import com.myhouse.biz.HouseService;
import com.myhouse.biz.MessageService;

import com.myhouse.biz.StreetService;
import com.myhouse.biz.TypeService;
import com.myhouse.entity.District;

import com.myhouse.entity.House;

import com.myhouse.entity.Message;

import com.myhouse.entity.Street;
import com.myhouse.entity.Type;
import com.myhouse.entity.User;

@Controller
public class HomeController {
	
	@Resource
	private HouseService houseService;
	@Resource
	private MessageService messageService;
	@Resource
	private DistrictService districtService;
	@Resource
	private StreetService streetService;
	@Resource
	private TypeService typeService;
	
	@RequestMapping("/index")
	public String index(Model model,HttpSession session){
		List<House> list = houseService.getNewHouse(0,1);
		List<House> list2 = houseService.getNewHouse(1,4);
		List<House> priceHouse = houseService.getPriceHouse(3);
		List<House> list3 = houseService.getNewHouse(0,2);
		List<House> list4 = houseService.getNewHouse(0,12);
		model.addAttribute("house",list);
		model.addAttribute("houses",list2);
		model.addAttribute("priceHouse",priceHouse);
		model.addAttribute("list3",list3);
		model.addAttribute("list4",list4);
		User user = (User)session.getAttribute("user");
		if(user!=null) {
			List<Message> messages = messageService.getMessageFromOrTo(user.getId(),null);
			model.addAttribute("messages", messages);
		}
		
		return "index";
	}

	
	@RequestMapping("/house-list")
	public String houseTime(Integer did,Integer sid,Integer tid,String title,Integer page,
			String price,String updateTime,Integer price1,Integer price2,Model model) {
		did = did==null?0:did;
		sid = sid==null?0:sid;
		tid = tid==null?0:tid;
		page = page==null?1:page;
		int pageSize = 4;
		int startPage = (page-1)*pageSize;
		price1 = price1==null?0:price1;
		price2 = price2==null?0:price2;
		List<House> houses = houseService.houseTime(did, sid, tid, title, startPage, pageSize, price, updateTime, price1, price2);
		int count = houseService.getRows(did, sid, tid, title, price1, price2);
		int totalPage = (int)Math.ceil((double)count/pageSize);
		List<District> districts = districtService.getAll();
		if(did>0) {
			List<Street> streets = streetService.getdistId(did);
			model.addAttribute("streets", streets);
		}
		List<Type> types = typeService.getAll();
		model.addAttribute("houses",houses);
		model.addAttribute("did",did);
		model.addAttribute("sid",sid);
		model.addAttribute("tid",tid);
		model.addAttribute("page",page);
		model.addAttribute("totalPage",totalPage);
		model.addAttribute("title",title);
		model.addAttribute("districts",districts);
		model.addAttribute("price1",price1);
		model.addAttribute("price2",price2);
		model.addAttribute("types", types);
		model.addAttribute("price",price);
		model.addAttribute("updateTime",updateTime);
		return "house-list";
	}
	
	@RequestMapping("/user/list")
	public String userlist(HttpSession session,Model model,Integer page) {
		User user = (User)session.getAttribute("user");
		page = page==null?1:page;
		int pageSize = 3;
		int startPage = (page-1)*pageSize;
		List<House> list = houseService.getUseridHouses(user.getId(), startPage, pageSize);
		int count = houseService.getUseridRows(user.getId());
		int totalPage = (int)Math.ceil((double)count/pageSize);
		model.addAttribute("houses",list);
		model.addAttribute("totalPage",totalPage);
		model.addAttribute("page",page);
		return "user-list";
	}
	
	
	@RequestMapping("/house")
	public String house(Model model,Integer id) {
		House house = houseService.getByHouse(id);
		model.addAttribute("house",house);
		return "house";
	}
	
	@RequestMapping("/addMessage")
	@ResponseBody
	public void addMessage(@RequestBody Message message){
		System.out.print(message.getToUser().getId());
		message.setCreateTime(new Date());
		message.setIsRead(false);
		message.setIsDeletedByFormUser(true);
		message.setIsDeletedByToUser(true);
		message.setId(0);
		messageService.addMessage(message);	
	}
	
	
	///-------------------->测试用

	@RequestMapping("/xiaoxi")
	 @ResponseBody
	public List<Message> xiaoxi(Integer fromUserId){
		List<Message> messagelist =messageService.getMessageFromOrTo(fromUserId, null);	
		return messagelist;
	}
	
	
	
	
	
	@RequestMapping("/test")
	public String test(Model model,Integer id){
		List<Message> messagelist =messageService.getMessageFromOrTo(null, null);
		model.addAttribute("messagelist", messagelist);		
		return "test";
	}
	
	 //返回集合
	 @RequestMapping("/getMessages")
	 @ResponseBody //将该方法的返回值序列化为json字符串
	 public List<Message> getMessages(Integer id)
	 {		 
	  return messageService.getMessage(id);
	 }
	
	
	

}
